<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理员管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/oksub.css">
</head>
<body>
    <div class="ok-body">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>管理员管理</legend>
        </fieldset>
        <!--模糊搜索区域-->
        <div class="layui-row">
            <form class="layui-form layui-col-md12 ok-search">
                <input class="layui-input" placeholder="管理员账号ID" autocomplete="off" name="adminid">
                <input class="layui-input" placeholder="管理员姓名" autocomplete="off" name="realname">
                <button class="layui-btn" lay-submit="" lay-filter="search">
                    <i class="layui-icon layui-icon-search"></i>
                </button>
            </form>
        </div>
        <!--数据表格-->
        <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
    </div>
    <!--js逻辑-->
    <script src="../../lib/layui/layui.js"></script>
    <script src="../../js/model.js"></script>
    <script>
        layui.use(["element", "table", "form", "laydate", "okLayer", "okUtils", "okMock","okNprogress","jquery","okMd5"], function () {
            let table = layui.table;
            let form = layui.form;
            let okLayer = layui.okLayer;
            let okUtils = layui.okUtils;
            let okNprogress = layui.okNprogress;
            let $=layui.jquery;
            let okMd5 = layui.okMd5;

            // var admin = JSON.parse(window.sessionStorage.getItem("admin"));
            //
            // if(admin==null){
            //     okLayer.yellowSighMsg("登陆时间超时，请重新登录！", function () {
            //         window.sessionStorage.clear();
            //         window.location = ctx+"/pages/admin/login.html";
            //     });
            // }

            // 获取contentText
            const ctx = function () {
                let pathName = document.location.pathname;
                let index = pathName.substr(1).indexOf("/");
                let result = pathName.substr(0, index + 1);
                return result;
            }();
            okNprogress.start();
            let adminTable = table.render({
                even: true, //开启隔行背景
                elem: '#tableId',
                url: ctx + '/admin/getAdminByIdAndName',
                limit: 20,
                page: true,
                toolbar: true,
                toolbar: "#toolbarTpl",
                size: "sm",
                headers: {"token":sessionStorage.getItem("token")},
                response:{
                    statusCode:200,
                },
                cols: [[
                    {type: "checkbox", fixed: "left"},
                    {field: "adminid", title: "管理员账号ID", width: 200, sort: true},
                    {field: "realname", title: "管理员名字", width: 200, sort: true,edit:'text'},
                    {field: "type", title: "权限", width: 150, align: "center", templet: "#roleTpl"},
                    {field: "contactnumber", title: "联系电话", width: 150, sort: true,edit:'text'},
                    {field: "headportrait", title: "头像", width: 500, sort: true},
                    {field: "password", title: "密码", width: 200, sort: true,hide:true},
                    {title: "操作", width: 100, align: "center", fixed: "right", templet: "#operationTpl"}
                ]],
                done: function (res, curr, count) {
                    okNprogress.done();
                },error:function (r) {
                    okNprogress.done();
                    console.log("错误信息",r)
                }
            });

            form.on("submit(search)", function (data) {
                adminTable.reload({
                    where: data.field,
                    page: {curr: 1},
                    headers:{"token":sessionStorage.getItem("token")},
                });
                return false;
            });

            table.on("toolbar(tableFilter)", function (obj) {
                switch (obj.event) {
                    case "batchEnabled":
                        batchEnabled();
                        break;
                    case "batchDisabled":
                        batchDisabled();
                        break;
                    case "batchDel":
                        batchDel();
                        break;
                    case "add":
                        add();
                        break;
                }
            });

            table.on("tool(tableFilter)", function (obj) {
                let data = obj.data;
                switch (obj.event) {
                    case "edit":
                        edit(data);
                        break;
                    case "del":
                        del(data);
                        break;
                    case "resetPwd":
                        resetPwd(data);
                        break;
                }
            });

            function batchEnabled() {
                okLayer.open("批量添加管理员","admin-batchInsert.html", "90%", "90%", null, function () {
                    // 开始进度条
                    okNprogress.start();
                    adminTable.reload();
                    // 结束进度条
                    okNprogress.done();
                });
            }

            function batchDisabled() {
                okLayer.confirm("确定要批量修改吗？", function (index) {
                    layer.close(index);
                    var checkStatus = table.checkStatus("tableId");
                    var rows = checkStatus.data.length;
                    if (rows > 0) {
                        var idsStr=checkStatus.data;
                        if (idsStr) {
                            var array = [];
                            for (var i=0;i<rows;i++){
                                array.push(new Admin(checkStatus.data[i].adminid,checkStatus.data[i].realname,null,
                                    checkStatus.data[i].contactnumber,null,null));
                            }
                            $.ajax({
                                url:ctx+'/admin/batchUpdateAdmin',
                                type:'POST',
                                data:{"jsonArrayAdmin": JSON.stringify(array)},
                                dataType:'json',
                                error:function(){
                                    okLayer.redCrossMsg("服务器错误")
                                },
                                success:function(data){
                                    if (data.code == 200) {
                                        // 业务正常
                                        okUtils.tableSuccessMsg(data.msg);
                                    } else {
                                        // 业务异常
                                        okLayer.yellowSighMsg(data.msg)
                                    }
                                }
                            });
                        }
                        return idsStr;
                    } else {
                        layer.msg("未选择有效数据", {offset: "t", anim: 6});
                    }
                });
            }

            function batchDel() {
                    okLayer.confirm("确定要批量删除吗？", function (index) {
                        layer.close(index);
                        var checkStatus = table.checkStatus("tableId");
                        var rows = checkStatus.data.length;
                        if (rows > 0) {
                            var idsStr=checkStatus.data;
                            if (idsStr) {
                                var idsStr = "";
                                for (var i = 0; i < checkStatus.data.length; i++) {
                                    idsStr += checkStatus.data[i].adminid + ",";
                                }
                                $.ajax({
                                    url:ctx+'/admin/batchDelAdmin',
                                    type:'POST',
                                    data:{"admins": idsStr},
                                    dataType:'json',
                                    error:function(){
                                        okLayer.redCrossMsg("服务器错误")
                                    },
                                    success:function(data){
                                        if (data.code == 200) {
                                            // 业务正常
                                            okUtils.tableSuccessMsg(data.msg);
                                            okNprogress.done();
                                        } else {
                                            // 业务异常
                                            okLayer.yellowSighMsg(data.msg)
                                        }
                                    }
                                });
                            }
                            return idsStr;
                        } else {
                            layer.msg("未选择有效数据", {offset: "t", anim: 6});
                        }
                    });
            }

            function add() {
                okLayer.open("添加管理员", "admin-add.html", "90%", "90%", null, function () {
                    // 开始进度条
                    okNprogress.start();
                    adminTable.reload();
                    // 结束进度条
                    okNprogress.done();
                })
            }

            function edit(data) {
                okLayer.open("更新管理员", "admin-edit.html", "90%", "90%", function (layero) {
                    let iframeWin = window[layero.find("iframe")[0]["name"]];
                    iframeWin.initForm(data);
                }, function () {
                    // 开始进度条
                    okNprogress.start();
                    adminTable.reload();
                    // 结束进度条
                    okNprogress.done();
                })
            }

            function resetPwd(data) {
                okLayer.confirm("确定要重置密码吗？", function () {
                    data.password=okMd5(123456);
                    $.ajax({
                        url:ctx+'/admin/updateAdmin',
                        type:'POST',
                        data:data,
                        dataType:'json',
                        error:function(){
                            okLayer.redCrossMsg("服务器错误")
                        },
                        success:function(data){
                            if (data.code == 200) {
                                // 业务正常
                                okUtils.tableSuccessMsg(data.msg);
                                okNprogress.done();
                            } else {
                                // 业务异常
                                okLayer.yellowSighMsg(data.msg)
                            }
                        }
                    });
                })
            }

            function del(data) {
                okLayer.confirm("确定要删除吗？", function () {
                    $.ajax({
                        url:ctx+'/admin/delAdminById',
                        type:'POST',
                        data:{"adminId": data.adminid},
                        dataType:'json',
                        error:function(){
                            okLayer.redCrossMsg("服务器错误")
                        },
                        success:function(data){
                            if (data.code == 200) {
                                // 业务正常
                                okUtils.tableSuccessMsg(data.msg);
                                okNprogress.done();
                            } else {
                                // 业务异常
                                okLayer.yellowSighMsg(data.msg)
                            }
                        }
                    });
                })
            }
        })
    </script>
    <!-- 头工具栏模板 -->
    <script type="text/html" id="toolbarTpl">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchEnabled">批量添加</button>
            <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="batchDisabled">批量修改</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="add">添加管理员</button>
        </div>
    </script>
    <!-- 行工具栏模板 -->
    <script type="text/html" id="operationTpl">
        <a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
        <a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
        <a href="javascript:" title="重置密码" lay-event="resetPwd"><i class="ok-icon">&#xe73f;</i></a>
    </script>
    <script type="text/html" id="roleTpl">
        {{#  if(d.type>10){ }}
            超级管理员
        {{#  } else if(d.type<10) { }}
            普通管理员
        {{#  } }}
    </script>
</body>
</html>
